package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.entity.pojo.Categories;
import com.entity.pojo.Concept;
import com.entity.vo.ConceptVo;
import com.mapper.ConceptMapper;
import com.service.ConceptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chenmo
 * @since 2022-04-02
 */
@Service
public class ConceptServiceImpl extends ServiceImpl<ConceptMapper, Concept> implements ConceptService {
    @Resource
    private ConceptMapper conceptMapper;

    @Override
    public List<ConceptVo> getConceptsPageCondition(long current, long limit, ConceptVo conceptQuery) {
        Page<ConceptVo> pageCondition = new Page<>(current, limit);
        QueryWrapper<Categories> wrapper = new QueryWrapper<>();
        validateCondition(wrapper, conceptQuery);
        return conceptMapper.getConceptsPageCondition(pageCondition, wrapper);
    }

    private void validateCondition(QueryWrapper<Categories> wrapper, ConceptVo conceptQuery) {
        Integer categoryId = conceptQuery.getCategoryId();
        String courseName = conceptQuery.getCourseName();
        String conceptName = conceptQuery.getConceptName();
        String categoryName = conceptQuery.getCategoryName();
        String parentConceptName = conceptQuery.getParentConceptName();
        String propertiesKey = conceptQuery.getPropertiesKey();
        String propertiesValue = conceptQuery.getPropertiesValue();
        Integer isDeleted = conceptQuery.getIsDeleted();

        if (categoryId != null)
            wrapper.eq("concept.categoryId", categoryId);
        if (StringUtils.hasText(courseName))
            wrapper.like("courses.courseName ", courseName);
        if ( StringUtils.hasText(conceptName))
            wrapper.like("conceptName", conceptName);
        if ( StringUtils.hasText(categoryName))
            wrapper.like("categories.categoryName", categoryName);
        if ( StringUtils.hasText(parentConceptName))
            wrapper.like("parentConceptName", parentConceptName);
        if ( StringUtils.hasText(propertiesKey))
            wrapper.like("propertiesKey", propertiesKey);
        if ( StringUtils.hasText(propertiesValue))
            wrapper.like("propertiesValue", propertiesValue);
        if (isDeleted != null) {
            wrapper.eq("concept.isDeleted", 0);
        }
    }
}
